import { useCatchTimeStore } from '../stores/catchTime'

interface dateTime {
    yy: number,
    mm: number,
    dd: number,
    day: number,
    nowDate: string,
    nowWeek: string,
    timer: any,
    init: () => any,
    hearTime: () => any,
}

const catchTime: dateTime = {
    yy: 0,
    mm: 0,
    dd: 0,
    day: 0,
    nowDate: '',
    nowWeek: '',
    timer: null,
    init: () => {
        const arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        const date = new Date();
        catchTime.yy = date.getFullYear();
        catchTime.mm = date.getMonth() + 1;
        catchTime.dd = date.getDate();
        catchTime.day = date.getDay();
        catchTime.nowDate = catchTime.yy + '-' + catchTime.mm + '-' + catchTime.dd;
        catchTime.nowWeek = arr[catchTime.day];


        return {
            date: catchTime.nowDate,
            week: catchTime.nowWeek
        }

    },
    hearTime: () => {
        const { runTime } = useCatchTimeStore()
        if (catchTime.timer) {
            clearInterval(catchTime.timer)
        }
        catchTime.timer = setInterval(() => {
            runTime(new Date().toLocaleTimeString())
        }, 1000)
    }
}

export default catchTime